﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DatabaseManagement
{
    public partial class ManagerGUI : Form
    {
        public SqlConnection connection;
        SqlDataAdapter adap;
        DataSet ds;
        SqlCommandBuilder cmdBuilder;
        AboutBox aboutBox;

        public static string serverName = "AIDA\\SQLEXPRESS";

        public ManagerGUI()
        {
            InitializeComponent();
            treeView.ExpandAll();
            aboutBox = new AboutBox();
        }

        private void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (adap != null)
            {
                try
                {
                    adap.Update(ds, "table");
                    statusLabel.Text = "Status: Update to database successfully";
                    statusLabel.ForeColor = Color.Lime;
                }
                catch (Exception)
                {
                    statusLabel.Text = "Status: Failed to update to database";
                    statusLabel.ForeColor = Color.Red;
                }
            }

            try
            {
                if (e.Node.Name.CompareTo("gameListNode") == 0)
                {
                    String commandData = "SELECT * FROM GameList";

                    adap = new SqlDataAdapter(commandData, connection);
                    cmdBuilder = new SqlCommandBuilder(adap);
                    ds = new DataSet();
                    adap.Fill(ds, "table");
                    dataGridView.DataSource = ds.Tables["table"].DefaultView;
                }

                if (e.Node.Name.CompareTo("gameLogNode") == 0)
                {
                    String commandData = "SELECT * FROM GameLog";

                    adap = new SqlDataAdapter(commandData, connection);
                    cmdBuilder = new SqlCommandBuilder(adap);
                    ds = new DataSet();
                    adap.Fill(ds, "table");
                    dataGridView.DataSource = ds.Tables["table"].DefaultView;
                }

                if (e.Node.Name.CompareTo("accountListNode") == 0)
                {
                    String commandData = "SELECT * FROM Account";

                    adap = new SqlDataAdapter(commandData, connection);
                    cmdBuilder = new SqlCommandBuilder(adap);
                    ds = new DataSet();
                    adap.Fill(ds, "table");
                    dataGridView.DataSource = ds.Tables["table"].DefaultView;
                }

                if (e.Node.Name.CompareTo("accountStatNode") == 0)
                {
                    String commandData = "SELECT * FROM AccountStatistic";

                    adap = new SqlDataAdapter(commandData, connection);
                    cmdBuilder = new SqlCommandBuilder(adap);
                    ds = new DataSet();
                    adap.Fill(ds, "table");
                    dataGridView.DataSource = ds.Tables["table"].DefaultView;
                }

                if (e.Node.Name.CompareTo("accountLogNode") == 0)
                {
                    String commandData = "SELECT * FROM AccountLog";

                    adap = new SqlDataAdapter(commandData, connection);
                    cmdBuilder = new SqlCommandBuilder(adap);
                    ds = new DataSet();
                    adap.Fill(ds, "table");
                    dataGridView.DataSource = ds.Tables["table"].DefaultView;
                }

                if (e.Node.Name.CompareTo("allNode") == 0)
                {
                    String commandData = "SELECT * FROM Message";

                    adap = new SqlDataAdapter(commandData, connection);
                    cmdBuilder = new SqlCommandBuilder(adap);
                    ds = new DataSet();
                    adap.Fill(ds, "table");
                    dataGridView.DataSource = ds.Tables["table"].DefaultView;
                }

                if (e.Node.Name.CompareTo("deliveredNode") == 0)
                {
                    String commandData = "SELECT * FROM Message WHERE (Message.Status=1)";

                    adap = new SqlDataAdapter(commandData, connection);
                    cmdBuilder = new SqlCommandBuilder(adap);
                    ds = new DataSet();
                    adap.Fill(ds, "table");
                    dataGridView.DataSource = ds.Tables["table"].DefaultView;
                }

                if (e.Node.Name.CompareTo("pendingNode") == 0)
                {
                    String commandData = "SELECT * FROM Message WHERE (Message.Status=0)";

                    adap = new SqlDataAdapter(commandData, connection);
                    cmdBuilder = new SqlCommandBuilder(adap);
                    ds = new DataSet();
                    adap.Fill(ds, "table");
                    dataGridView.DataSource = ds.Tables["table"].DefaultView;
                }

                if (e.Node.Name.CompareTo("bugNode") == 0)
                {
                    String commandData = "SELECT * FROM BugReport";

                    adap = new SqlDataAdapter(commandData, connection);
                    cmdBuilder = new SqlCommandBuilder(adap);
                    ds = new DataSet();
                    adap.Fill(ds, "table");
                    dataGridView.DataSource = ds.Tables["table"].DefaultView;
                }
            }
            catch (Exception)
            {
                statusLabel.Text = "Status: Failed read from database";
                statusLabel.ForeColor = Color.Red;
                dataStatusLabel.Text = "Database: Offline";
                dataStatusLabel.ForeColor = Color.Red;

                MessageBox.Show("Failed to connect to database");
            }
        }

        private void ManagerGUI_Load(object sender, EventArgs e)
        {
            try
            {
                connection = new SqlConnection("Server=" + serverName + ";Database=Versus;Integrated Security=True");
                connection.Open();
            }
            catch (Exception)
            {
                statusLabel.Text = "Status: Failed read from database";
                statusLabel.ForeColor = Color.Red;
                dataStatusLabel.Text = "Database: Offline";
                dataStatusLabel.ForeColor = Color.Red;
                MessageBox.Show("Failed to connect to database");
            }
        }

        private void ManagerGUI_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (connection!=null) connection.Close();
        }

        private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
        }

        private void dataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show("Invalid Entry!");
        }

        private void reconnectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                connection = new SqlConnection("Server=" + serverName + ";Database=Versus;Integrated Security=True");
                connection.Open();
                statusLabel.Text = "Status: Ready";
                statusLabel.ForeColor = Color.Lime;
                dataStatusLabel.Text = "Database: Online";
                dataStatusLabel.ForeColor = Color.Lime;
            }
            catch (Exception)
            {
                statusLabel.Text = "Status: Failed to read from database";
                statusLabel.ForeColor = Color.Red;
                dataStatusLabel.Text = "Database: Offline";
                dataStatusLabel.ForeColor = Color.Red;
                MessageBox.Show("Failed to connect to database");
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {

        }

        private void aboutUsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            aboutBox.Show();
        }
    }
}
